<?php
interface IWoman{
    public function getType();
    public function getRequest();
}

class Woman implements IWoman{
    private $type=0;
    private $request;

    public function __construct($type,$request){
        $this->type=$type;

        switch($this->type){
            case 1:
                $this->request="女儿的请求是：".$request."</br>";
                break;
            case 2:
                $this->request="妻子的请求是：".$request."</br>";
                break;
            case 3:
                $this->request="女儿的请求是：".$request."</br>";
                break;
        }
    }


    public function getType()
    {
        return $this->type;
    }

    public function getRequest()
    {
        return $this->request;
    }

}

abstract class Handler{
    private $level;
    private $nextHandler;

    public function __construct($level){
        $this->level=$level;
    }

    public function setNextHandler(Handler $handler){
        $this->nextHandler=$handler;
    }

    public function hasNext(){
        if($this->nextHandler){
            return true;
        }else{
            return false;
        }
    }

    public function handlerMessage(Woman $woman){
        if($this->level===$woman->getType()){
            $this->response($woman->getRequest());
        }elseif($this->hasNext()){
            $this->nextHandler->response($woman->getRequest());
        }else{
            echo "不处理！</br>";
        }
    }

    public abstract function response($requst);

}

class Father extends Handler{

    public function __construct(){
        parent::__construct(1);
    }

    public function response($requst){
        echo "**************************女儿向父亲提出请求****************************</br>";
        echo $requst;
        echo "父亲回应：同意</br>";
    }
}

class Husband extends Handler{

    public function __construct(){
        parent::__construct(2);
    }

    public function response($requst){
        echo "**************************妻子向丈夫提出请求****************************</br>";
        echo $requst;
        echo "丈夫回应：同意</br>";
    }
}

class Son extends Handler{
    public function __construct(){
        parent::__construct(3);
    }

    public function response($requst){
        echo "**************************母亲向儿子提出请求****************************</br>";
        echo $requst;
        echo "儿子回应：同意</br>";
    }
}

$father=new Father(1);
$husband=new Husband(2);
$son=new Son(3);

$father->setNextHandler($husband);
$father->setNextHandler($son);

echo "<meta charset=UTF-8>";
for($i=0;$i<5;$i++){
    $woman=new Woman(rand(1,3),"我要出去～～～");
    $father->handlerMessage($woman);
}

?>